package de.szut.traineeslife.taks;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.widget.Toast;

import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.File;

import de.szut.traineeslife.utils.DriveHelper;
import de.szut.traineeslife.utils.FileHelper;

public class UploadToDriveTask extends AsyncTask<Void, Void, Void> {

	private ProgressDialog dialog;
	private final Uri mFileUri;
	private Drive mService;
	private FileHelper mFileHelper;
	private File driveFile;
	private Context mContext;
	private DriveHelper mDriveHelper;

	/**
	 * Task to upload Files to Users Drive-account
	 * 
	 * @param context
	 * @param service
	 * @param fileUri
	 * @param helper
	 */
	//SelectionListener listener
	public UploadToDriveTask(Context context, Drive service, Uri fileUri, FileHelper helper, DriveHelper driveHelper) {
		mFileUri = fileUri;
		mService = service;
		mFileHelper = helper;
		mContext = context;
//		mListener = listener;
		mDriveHelper = driveHelper;
	}

	@Override
	protected Void doInBackground(Void... params) {
		try {
			driveFile = mDriveHelper.saveFileToDrive(this.mFileUri, mService, mFileHelper, "TraineesLife_Vorlage");
			Thread.sleep(2000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		if (dialog != null) {
			dialog.dismiss();
		}
		if (driveFile == null) {
			Toast.makeText(mContext, "Upload Fehlgeschlagenm, bitte versuchen sie es erneut", Toast.LENGTH_LONG).show();
		} 
//		else {
//			mListener.uploadFinished(driveFile);
//		}
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		dialog = new ProgressDialog(mContext);
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		dialog.setMessage("Upload zu Drive....");
		dialog.setCancelable(false);
		dialog.show();
	}
}
